<template>
  <div class="main-menu">
    <!-- 1.logo -->
    <div class="logo">
      <img class="img" src="@/assets/img/logo.svg" alt="" />
      <h2 v-show="!isFold" class="title">巡检后台管理系统</h2>
    </div>

    <!-- 
        el-menu组件的分析
        1.el-menu 整菜单
        2.el-sub-menu 可以有子菜单，并且是可以展开的
        3.el-menu-item-group 
          对子菜单进行分组的，但是不能展开
          目的是给小菜单加上组的名称，但是不能交互
          #title插槽 表示一个标题
        4.el-menu-item 
          可以点击每一个item
     -->
    <div class="menu">
      <el-menu
        :default-active="defaultActive"
        :collapse="isFold"
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
      >
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 
                以前版本利用动态class就能展示出动态的图标
                但现在的图标是用组件的方式展示的
                <el-icon><EditPen /></el-icon>
                字符串: el-icon-monitor => 组件 component动态组件 
              -->
              <el-icon>
                <component :is="item.icon" />
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subItem in item.children" :key="subItem.id">
              <!-- 只有两层 -->
              <el-menu-item
                v-if="subItem.children === null"
                :index="subItem.id + ''"
                @click="handleItemClick(subItem)"
              >
                {{ subItem.name }}</el-menu-item
              >
              <!-- 有三层 -->
              <el-sub-menu v-else :index="subItem.id + ''">
                <template #title>
                  <el-icon v-if="subItem.icon">
                    <component :is="subItem.icon" />
                  </el-icon>
                  <span>{{ subItem.name }}</span>
                </template>
                <template v-for="thirdItem in subItem.children" :key="thirdItem.id">
                  <el-menu-item :index="thirdItem.id + ''" @click="handleItemClick(thirdItem)">
                    {{ thirdItem.name }}</el-menu-item
                  >
                </template>
              </el-sub-menu>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue"
import { useRoute, useRouter } from "vue-router"
import { mapPathToMenu } from "@/utils/map-menus"
// 引入菜单
import userMenus from "@/mock/userMenus"

// 定义props
defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})

// 1.获取动态的菜单
// 2.监听item的点击
const router = useRouter()
function handleItemClick(item: any) {
  const url = item.url
  router.push(url)
}

// 3.ElMenu的默认菜单
// 拿到路由的信息
const route = useRoute()
// console.log(route)
// 匹配到当前路径对应的menu
const pathMenu = mapPathToMenu(route.path, userMenus)
const defaultActive = ref(pathMenu.id + "")
</script>

<style lang="less" scoped>
.main-menu {
  height: 100%;
  background-color: #001529;
}

.logo {
  display: flex;
  height: 28px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;

  .img {
    height: 100%;
    margin: 0 10px;
  }

  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}

.el-menu {
  border-right: none;
  //控制用户能否选中文本
  user-select: none;
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
